import axios from "axios";

// 创建axios实例
const request = axios.create({
  baseURL: '/',
  timeout: 100000,
  // 表示跨域请求时是否需要使用凭证
  withCredentials: true,
  headers: {
    Accept: "application/json, text/plain, */*",
    "Content-Type": "application/json;charset=utf-8"
  }
});

// 请求拦截
service.interceptors.request.use(
  (config) => {
    config.params = {
      ...config.params,
      timestamp: Date.now(),
    };
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
service.interceptors.response.use(
  (response) => {
    const { data } = response;
    if (data.code == 200) {
      return response;
    }
    return Promise.resolve(response);
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 导出axios实例
export default request;
